from app.auth.models.api import Api, ApiCreate
from app.core.handlers.database import SessionDep


class ApiCRUD:
    def __init__(self, db: SessionDep):
        self.db = db

    async def get_api_by_code(self, code: str) -> Api | None:
        query = self.db.select(Api).where(Api.code == code)
        return self.db.scalar(query)

    async def create_api(self, api_data: ApiCreate) -> Api:
        api = Api(**api_data.model_dump())
        self.db.add(api)
        return api
